<template>
  <div class="chat-container">
<!--    处方信息-->
      <el-form label-width="80px" :model="formLabelAlign">
        <el-form-item label="患者id">
          <el-input v-model="formLabelAlign.patientId"></el-input>
        </el-form-item>
        <el-form-item label="患者姓名">
          <el-input v-model="formLabelAlign.name"></el-input>
        </el-form-item>
        <el-form-item label="患者年龄">
          <el-input v-model="formLabelAlign.age"></el-input>
        </el-form-item>
        <el-form-item label="患者性别">
          <el-input v-model="formLabelAlign.sex"></el-input>
        </el-form-item>
        <el-form-item label="诊断id:">
          <el-input v-model="formLabelAlign.prescriptionId"></el-input>
        </el-form-item>
        <el-form-item label="诊断名称:">
          <el-input v-model="formLabelAlign.diagnosis"></el-input>
          <el-button @click="diagnosticListButton() ">诊断列表按钮</el-button>
        </el-form-item>
        <el-form-item label="药品id">
          <el-input v-model="formLabelAlign.restorativesIds"></el-input>
        </el-form-item>
        <el-form-item label="药品名称">
          <el-input v-model="formLabelAlign.drugNames"></el-input>
          <el-button @click="drugListButton()">药品列表按钮</el-button>
        </el-form-item>
      </el-form>

<!--    药品对话框-->
    <el-dialog title="药品列表" :visible.sync="dialogVisible" >
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="药品id" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.restorativesId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="药品名称" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.drugName }}</span>
          </template>
        </el-table-column>
        <el-table-column>
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </el-dialog>

    <!--      症状列表对话框-->
    <el-dialog title="症状列表" :visible.sync="symptomList" >
      <el-table :data="tableData2" style="width: 100%">
        <el-table-column label="药品id" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.prescriptionId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="药品名称" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.diagnosis }}</span>
          </template>
        </el-table-column>
        <el-table-column>
          <template slot-scope="scope">
            <el-button @click="handleClick2(scope.row)" type="text" size="small">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
        <el-button @click="symptomList = false">取 消</el-button>
        <el-button type="primary" @click="symptomList = false">确 定</el-button>
      </el-dialog>

    <el-button @click="sendAPrescription()">发送处方</el-button>
    <el-button @click="sendAPrescription2()">发送处方(测试)</el-button>

  </div>
</template>

<script>

import {listOfCommonDrugs} from "@/api/doctorTerminal/myPharmacy/commonlyUsedDrugs";
import {
  makeAPrescriptionAdd,
  queryPatientInformationById,
  seeYourDoctorForSymptoms
} from "@/api/doctorTerminal/myPharmacy/writeAPrescription";

export default {
  data() {
    return {
      // 症状列表对话框
      symptomList: false,
      // 药品列表对话框
      dialogVisible: false,
      // 开处方对象
      formLabelAlign: {
        // 主键id
        id: 0,
        // 患者id
        patientId:1,
        // 患者名称
        name: '',
        // 年龄
        age: 0,
        // 性别
        sex: '',
        // 病症id
        prescriptionId: 0,
        // 病症名称
        diagnosis: '',
        // list药品id
        restorativesIds: [],
        // list药品名称
        drugNames: [],
        // 医生id
        doctorId: this.$store.state.user.id,
      },
      // 药品列表
      tableData: [],
      // 症状列表
      tableData2: [],
      // 医生id
      doctorId: this.$store.state.user.id
    }
  },
  created() {
    this.getPatient()
  },
  methods: {
    // 药品列表
    getData() {
      listOfCommonDrugs(this.doctorId).then(res =>{
        console.log("药品列表: " , res)
        this.tableData = res
      })
    },
    // 症状列表
    getSymptom() {
      seeYourDoctorForSymptoms(this.doctorId).then(res =>{
        console.log("症状列表: " , res)
        this.tableData2 = res
      })
    },
    // 患者信息
    getPatient() {
      queryPatientInformationById(1).then(res =>{
        console.log("患者信息: " , res)
        this.formLabelAlign.patientId = res.patientInformationId
        this.formLabelAlign.name = res.name
        this.formLabelAlign.age = res.age
        this.formLabelAlign.sex = res.sex
      })
    },
    /** 添加药品 **/
    handleClick(row) {
      this.formLabelAlign.restorativesIds.push(row.restorativesId)
      this.formLabelAlign.drugNames.push(row.drugName)
    },
    /** 添加症状 */
    handleClick2(row) {
      this.formLabelAlign.prescriptionId = row.prescriptionId
      this.formLabelAlign.diagnosis = row.diagnosis
    },
    // 诊断列表按钮
    diagnosticListButton() {
      this.getSymptom()
      this.symptomList = true;
    },
    // 药品列表按钮
    drugListButton() {
      this.getData()
      this. dialogVisible = true;
    },
    // 发送处方
    sendAPrescription() {
      makeAPrescriptionAdd(this.formLabelAlign).then(res =>{
        console.log("发送处方后返回的数据: ",res.data)
        // 将药物名称数组转换为逗号分隔的字符串
        const drugNamesString = res.data.drugName.join(',');
        this.$router.push({
          path: '/im',
          query: {
            id: this.formLabelAlign.patientId,
            ids: res.data.id,
            diagnosis: res.data.diagnosis,
            drugName: drugNamesString
          }
        })
      })
    },
    // 测试
    sendAPrescription2() {
        this.$router.push({
          path: '/im',
          query: {
            id: this.formLabelAlign.patientId,
            ids: 9,
            diagnosis: "脑血管病",
            drugName: "999皮炎领1"
          }
        })
    },
  }
}
</script>

<style scoped>

</style>
